package com.imedway.ihe.dtz.xds.repository;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;
import javax.xml.namespace.QName;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLProvideAndRegisterDocumentSetRequest30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryError30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssociationType;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.AssociationType1;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ExtrinsicObjectType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryPackageType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
import org.openehealth.ipf.commons.ihe.xds.iti42.Iti42PortType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.imedway.ihe.dtz.xds.cons.ConstProperties;
import com.imedway.ihe.dtz.xds.cons.HasMemberCons;
import com.imedway.ihe.dtz.xds.cons.SysConsts;
import com.imedway.ihe.dtz.xds.dao.DataDao;
import com.imedway.ihe.dtz.xds.dto.XdsDto;
import com.imedway.ihe.dtz.xds.entity.IheXdsFileInfo;
import com.imedway.ihe.dtz.xds.util.DhccFileUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class Iti41Repository {

	@Autowired
	private ConstProperties constProperties;

	@SuppressWarnings("static-access")
	public RegistryResponseType run(ProvideAndRegisterDocumentSetRequestType body) {
				
		RegistryResponseType registryResponseType = new RegistryResponseType();
//		registryResponseType.setRegistryErrorList(new RegistryErrorList());
		List<EbXMLRegistryError> registryError = new ArrayList<EbXMLRegistryError>();
		EbXMLRegistryResponse30 ebXMLRegistryResponse30 = new EbXMLRegistryResponse30(registryResponseType);
		boolean flag = true;
		try {

			EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody = new EbXMLProvideAndRegisterDocumentSetRequest30(
					body);

			// 获取提交集（一次事务一个提交集）
			List<EbXMLRegistryPackage> ebXMLRegistryPackages = ebXMLBody
					.getRegistryPackages(SysConsts.RegistryPackageSubmissionSetClassificationNode);
			if (ebXMLRegistryPackages != null && ebXMLRegistryPackages.size() > 0) {
				for (EbXMLRegistryPackage ebXMLRegistryPackage : ebXMLRegistryPackages) {
					// 获取提交集相关的id
					String registryPackageId = ebXMLRegistryPackage.getId();
					// 通过提交集id获取关联类型
					List<EbXMLAssociation> ebXMLAssociations = ebXMLBody.getAssociations();
					if (ebXMLAssociations != null && ebXMLAssociations.size() > 0) {
						for (EbXMLAssociation ebXMLAssociation : ebXMLAssociations) {
							if (registryPackageId.equals(ebXMLAssociation.getSource())) {
								EbXMLAssociation association = ebXMLAssociation;
								AvailabilityStatus availabilityStatus =	association.getStatus();
								if(availabilityStatus.DEPRECATED.equals(availabilityStatus)){
									genError(registryError, Severity.ERROR, ErrorCode.INVALID_REQUEST_EXCEPTION.getOpcode(),"请求状态异常");
									break;
								}else{
									String method =	determineWhichMethod(association,ebXMLBody);
									switch (method) {
									case HasMemberCons.SSDEHasmember:
										//新增文档到默认路径
										this.documentSSDEHasmember(registryError,association,  ebXMLBody);
										break;
									case HasMemberCons.SSFDHasmember:
										//新建文件夹到默认路径
										this.SSFDHasmember(registryError,ebXMLAssociation,ebXMLBody);
										break;
									case "documentToFolderSSFDHasmember":
										//增加文件到文件夹
									    this.documentToFolderSSFDHasmember(registryError,ebXMLAssociation,ebXMLBody);
										break;
									case "documentAPPEND":
										//追加
										this.documentAPPEND(registryError,ebXMLAssociation,ebXMLBody);
										break;										
									case "documentTRANSFORM":
										//转换
										this.documentTRANSFORM(registryError,ebXMLAssociation,ebXMLBody);
										break;
									case "documentREPLACE":
										//替换
										this.documentREPLACE(registryError,ebXMLAssociation,ebXMLBody);
										break;
									case "documentSIGNS":
										// Document 签名
										this.documentSIGNS(registryError,ebXMLAssociation,ebXMLBody);
										break;
//									case IS_SNAPSHOT_OF:
//										// Document 快照
//										flag = this.documentIS_SNAPSHOT_OF(ebXMLAssociation,ebXMLBody);
//										break;
//									case UPDATE_AVAILABILITY_STATUS:
//										// Document 更新状态
//										flag = this.documentUPDATE_AVAILABILITY_STATUS(ebXMLAssociation,ebXMLBody);
//										break;
//									case TRANSFORM_AND_REPLACE:
//										// Document 转换替换
//										flag = this.documentTRANSFORM_AND_REPLACE(ebXMLAssociation,ebXMLBody);
//										break;
									default:
										break;
									}
								}
																										
							} else {
								genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
										"缺失提交集关联关系");
							}
						}
					} else {
						genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
								"缺失提交集关联关系");
					}
				}
			} else {
				genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(), "缺失提交集");
			}
						
		} catch (Exception e) {
			genError(registryError, Severity.ERROR, ErrorCode.INVALID_REQUEST_EXCEPTION.getOpcode(),e.getMessage());
			log.debug(e.getMessage(), e);
		}
		if(registryError!=null&&registryError.size()>0) {
			flag = false;
			ebXMLRegistryResponse30.setErrors(registryError);
		}
		ebXMLRegistryResponse30.setStatus(flag ? Status.SUCCESS : Status.FAILURE);
//		ebXMLRegistryResponse30.setStatus(Status.FAILURE);
		
		return ebXMLRegistryResponse30.getInternal();
		
		
	}
	
	private void genError(List<EbXMLRegistryError> registryError,Severity severity,String errorCode,String codeContext) {
		EbXMLRegistryError30 re = new EbXMLRegistryError30(new RegistryError());
		re.setSeverity(severity);
		re.setErrorCode(errorCode);
		re.setCodeContext(codeContext);
		registryError.add(re);
	}
	
	public boolean iti42(SubmitObjectsRequest submitObjectsRequest) {
		boolean flag = true;
		URL wsdlUrl;
		try {
			wsdlUrl = new URL("http://localhost:8080/services/iti42?wsdl");
			QName serviceName = new QName("http://service.ihe.imedway.com/", "Iti42PortTypeImplService");
			javax.xml.ws.Service service = javax.xml.ws.Service.create(wsdlUrl, serviceName);
			Iti42PortType myWebService = service.getPort(Iti42PortType.class);
			RegistryResponseType registryResponseType = myWebService
					.documentRegistryRegisterDocumentSetB(submitObjectsRequest);
			log.info(registryResponseType.getRequestId());

		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public String determineWhichMethod(EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		String method = null;
		AssociationType associationType = ebXMLAssociation.getAssociationType();		
		EbXMLObjectLibrary ebXMLObjectLibrary =	ebXMLBody.getObjectLibrary();
		
		Object object = ebXMLObjectLibrary.getById(ebXMLAssociation.getTarget());
		
		if(associationType.equals(AssociationType.HAS_MEMBER)) {
			if(object instanceof ExtrinsicObjectType) {
				List<String> submissionSetStatus = ebXMLAssociation.getSlotValues("SubmissionSetStatus");
				if(submissionSetStatus!=null&&"Original".equalsIgnoreCase(submissionSetStatus.get(0))){
					method = HasMemberCons.SSDEHasmember;
				}else {
					log.info("需要新增处理方法");
				}
			}else if(object instanceof RegistryPackageType) {
				method = HasMemberCons.SSFDHasmember;
			}else if(object instanceof AssociationType1) {
				AssociationType1 associationType1 = (AssociationType1)object;
				if(AssociationType.HAS_MEMBER.getOpcode30().equals(associationType1.getAssociationType())){
					Object objectSource = ebXMLObjectLibrary.getById(ebXMLAssociation.getSource());
					Object objectTarget = ebXMLObjectLibrary.getById(ebXMLAssociation.getTarget());
					if(objectSource instanceof RegistryPackageType&&objectTarget instanceof ExtrinsicObjectType) {
						method = "documentToFolderSSFDHasmember";
					}else {
						log.info("需要新增处理方法");
					}
				}else if(AssociationType.APPEND.getOpcode30().equals(associationType1.getAssociationType())) {	
					method = "documentAPPEND";
				}else if(AssociationType.REPLACE.getOpcode30().equals(associationType1.getAssociationType())) {	
					method = "documentREPLACE";
				}else if(AssociationType.TRANSFORM.getOpcode30().equals(associationType1.getAssociationType())) {	
					method = "documentTRANSFORM";
				}else if(AssociationType.SIGNS.getOpcode30().equals(associationType1.getAssociationType())) {	
					method = "documentSIGNS";
				}
			}
		}else {
			log.info("提交集的关联关系未支持:"+associationType.getOpcode30());
		}
		
		return method;
	}

   //新建文件
	private boolean documentSSDEHasmember(List<EbXMLRegistryError> registryError,EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		boolean flag = true;
		log.info("documentSSDEHasmember");
		try {	
			
			EbXMLExtrinsicObject ebXMLExtrinsicObject = getFileMineTypeByDocumentID(ebXMLAssociation.getTarget(), ebXMLBody.getExtrinsicObjects());						
			String XDSSubmissionSetpatientId = ebXMLExtrinsicObject.getExternalIdentifierValue(SysConsts.XDSDocumentEntrypatientIdidentificationScheme);
			log.debug(XDSSubmissionSetpatientId);
			//判断是否存在改患者
			if(find(XDSSubmissionSetpatientId).isEmpty()) {
				genError(registryError, Severity.ERROR, ErrorCode.UNKNOWN_PATIENT_ID.getOpcode(),"患者不存在");
				return false;
			}
			
			EbXMLRegistryPackage ebXMLRegistryPackage = getEbXMLRegistryPackageByID(ebXMLAssociation.getSource(), ebXMLBody.getRegistryPackages());
			String xDSSubmissionSetpatientIdString = ebXMLRegistryPackage.getExternalIdentifierValue(SysConsts.XDSSubmissionSetpatientIdidentificationScheme);
			if(!XDSSubmissionSetpatientId.equals(xDSSubmissionSetpatientIdString)) {
				genError(registryError, Severity.ERROR, ErrorCode.PATIENT_ID_DOES_NOT_MATCH.getOpcode(),"请求的提交集患者id不一致");
				return false;
			}
			
			
			DataHandler dataHandler =ebXMLBody.getDocuments().get(ebXMLAssociation.getTarget());					
			InputStream inputStream = dataHandler.getInputStream();
//			String uuidString=UUID.fastUUID().toString();
			String uuidString = ebXMLExtrinsicObject.getId();
			String filePath = constProperties.getFileRootPath() + uuidString+"."+	DhccFileUtils.getFileType(ebXMLExtrinsicObject.getMimeType());
			log.info(filePath);
			DhccFileUtils.saveToFile(inputStream, new File(filePath));
					
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
	
		return flag;
	}

	
	//新增文件夹
	private boolean SSFDHasmember(List<EbXMLRegistryError> registryError,EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("SSFDHasmember");
		boolean flag = true;
		try {		
			//新增文件夹(根目录下一级关系)
			EbXMLRegistryPackage ebXMLRegistryPackage = getEbXMLRegistryPackageByID(ebXMLAssociation.getTarget(),ebXMLBody.getRegistryPackages());						
			String folderName = ebXMLRegistryPackage.getName().getValue();
			if(folderName.isEmpty()) {
				genError(registryError, Severity.ERROR, ErrorCode.INVALID_REQUEST_EXCEPTION.getOpcode(),"文件夹未命名");
				return false;
			}
			DhccFileUtils.createDirectory(constProperties.getFileRootPath()+folderName);
		
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	
	private boolean documentToFolderSSFDHasmember(List<EbXMLRegistryError> registryError,EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentToFolderSSFDHasmember");
		boolean flag = true;
		try {		
			//新增文件夹(根目录下一级关系)
			EbXMLAssociation documentToFolderAssociation=getEbXMLAssociationByID(ebXMLAssociation.getTarget(),ebXMLBody.getAssociations());
			String folderId = documentToFolderAssociation.getSource();
			String documentId = documentToFolderAssociation.getTarget();

			//folder
			EbXMLRegistryPackage ebXMLRegistryPackage = getEbXMLRegistryPackageByID(folderId,ebXMLBody.getRegistryPackages());						
			String folderName = ebXMLRegistryPackage.getName().getValue();
			if(folderName.isEmpty()) {
				genError(registryError, Severity.ERROR, ErrorCode.INVALID_REQUEST_EXCEPTION.getOpcode(),"文件夹未命名");
				return false;
			}
			//如果文件夹不存在直接新建
			DhccFileUtils.createDirectory(constProperties.getFileRootPath()+folderName);
		  			
			//新增文件到文件夹
			EbXMLExtrinsicObject ebXMLExtrinsicObject = getFileMineTypeByDocumentID(documentId, ebXMLBody.getExtrinsicObjects());
			DataHandler dataHandler =ebXMLBody.getDocuments().get(ebXMLAssociation.getTarget());	
			InputStream inputStream = dataHandler.getInputStream();
			String filePath = constProperties.getFileRootPath() + folderName+"\\"+ebXMLExtrinsicObject.getId()+"."+
			     DhccFileUtils.getFileType(ebXMLExtrinsicObject.getMimeType());
			log.info(filePath);
			DhccFileUtils.saveToFile(inputStream, new File(filePath));
				
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	
	public boolean documentREPLACE(List<EbXMLRegistryError> registryError,EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentREPLACE");
		boolean flag = true;
		try {
			EbXMLObjectLibrary ebXMLObjectLibrary =	ebXMLBody.getObjectLibrary();			
			Object object = ebXMLObjectLibrary.getById(ebXMLAssociation.getTarget());
			EbXMLAssociation associationType1 = (EbXMLAssociation)object;
						
			EbXMLExtrinsicObject ebXMLExtrinsicObjectTarget = getFileMineTypeByDocumentID(associationType1.getTarget(), ebXMLBody.getExtrinsicObjects());
			EbXMLExtrinsicObject ebXMLExtrinsicObjectSource = getFileMineTypeByDocumentID(associationType1.getSource(), ebXMLBody.getExtrinsicObjects());

			String oldFile=constProperties.getFileRootPath() + ebXMLExtrinsicObjectSource.getId()+"."+DhccFileUtils.getFileType(ebXMLExtrinsicObjectSource.getMimeType());			
			String newFile = constProperties.getFileRootPath() + ebXMLExtrinsicObjectTarget.getId()+"."+DhccFileUtils.getFileType(ebXMLExtrinsicObjectTarget.getMimeType());				
			DataHandler dataHandler =ebXMLBody.getDocuments().get(ebXMLAssociation.getTarget());					
			InputStream inputStream = dataHandler.getInputStream();
			log.info(newFile);
			DhccFileUtils.replaceToFile(inputStream, new File(newFile), new File(oldFile));
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
	
		return flag;
	}

	public boolean documentAPPEND(List<EbXMLRegistryError> registryError,EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentAPPEND");
		boolean flag = true;
		try {
			EbXMLObjectLibrary ebXMLObjectLibrary =	ebXMLBody.getObjectLibrary();			
			Object object = ebXMLObjectLibrary.getById(ebXMLAssociation.getTarget());
			EbXMLAssociation associationType1 = (EbXMLAssociation)object;
						
			EbXMLExtrinsicObject ebXMLExtrinsicObjectSource = getFileMineTypeByDocumentID(associationType1.getSource(), ebXMLBody.getExtrinsicObjects());
			
			String oldFile=constProperties.getFileRootPath() + ebXMLExtrinsicObjectSource.getId()+"."+DhccFileUtils.getFileType(ebXMLExtrinsicObjectSource.getMimeType());	
				
			DataHandler dataHandler =ebXMLBody.getDocuments().get(ebXMLAssociation.getTarget());					
			InputStream inputStream = dataHandler.getInputStream();
			
			log.info(oldFile);
			
			DhccFileUtils.appendToFile(inputStream, new File(oldFile));
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return flag;
	}

	public boolean documentTRANSFORM(List<EbXMLRegistryError> registryError,EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentTRANSFORM");
		boolean flag = true;
		try {
			EbXMLObjectLibrary ebXMLObjectLibrary =	ebXMLBody.getObjectLibrary();			
			Object object = ebXMLObjectLibrary.getById(ebXMLAssociation.getTarget());
			EbXMLAssociation associationType1 = (EbXMLAssociation)object;
						
			EbXMLExtrinsicObject ebXMLExtrinsicObjectTarget = getFileMineTypeByDocumentID(associationType1.getTarget(), ebXMLBody.getExtrinsicObjects());
			EbXMLExtrinsicObject ebXMLExtrinsicObjectSource = getFileMineTypeByDocumentID(associationType1.getSource(), ebXMLBody.getExtrinsicObjects());

			String oldFile=constProperties.getFileRootPath() + ebXMLExtrinsicObjectSource.getId()+"."+DhccFileUtils.getFileType(ebXMLExtrinsicObjectSource.getMimeType());			
			String newFile = constProperties.getFileRootPath() + ebXMLExtrinsicObjectTarget.getId()+"."+DhccFileUtils.getFileType(ebXMLExtrinsicObjectTarget.getMimeType());	
					
			DataHandler dataHandler =ebXMLBody.getDocuments().get(ebXMLAssociation.getTarget());					
			InputStream inputStream = dataHandler.getInputStream();
			log.info(newFile);
			DhccFileUtils.transformToFile(inputStream, new File(newFile));
									
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return flag;
	}


	public boolean documentSIGNS(List<EbXMLRegistryError> registryError,EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentSIGNS");
		boolean flag = true;
		try {
			//Details of how Digital Signatures are represented are found in the ITI Document Digital Signature (DSG) Profile.
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	public boolean documentIS_SNAPSHOT_OF(EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentIS_SNAPSHOT_OF");
		boolean flag = true;
		try {
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	public boolean documentUPDATE_AVAILABILITY_STATUS(EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentUPDATE_AVAILABILITY_STATUS");
		boolean flag = true;
		try {
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	public boolean documentTRANSFORM_AND_REPLACE(EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("documentTRANSFORM_AND_REPLACE");
		boolean flag = true;
		try {
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	
	public boolean isOrNotExitedPatient(EbXMLAssociation ebXMLAssociation, EbXMLProvideAndRegisterDocumentSetRequest30 ebXMLBody) {
		log.info("isOrNotExitedPatient");
		boolean flag = true;
		try {
			EbXMLExtrinsicObject ebXMLExtrinsicObject = getFileMineTypeByDocumentID(ebXMLAssociation.getTarget(), ebXMLBody.getExtrinsicObjects());
			String XDSSubmissionSetpatientId = ebXMLExtrinsicObject.getExternalIdentifierValue(SysConsts.XDSDocumentEntrypatientIdidentificationScheme);
			List<IheXdsFileInfo> iheXdsInfoFileList=this.find(XDSSubmissionSetpatientId);
			if(iheXdsInfoFileList.isEmpty()) {
				flag = false;
			}
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	
	public List<IheXdsFileInfo> find(String patientId) throws SQLException {
		XdsDto xdsDto = new XdsDto();
		List<IheXdsFileInfo> iheXdsInfoFileList = new ArrayList<IheXdsFileInfo>();
		IheXdsFileInfo iheXdsFileInfo =new IheXdsFileInfo();
		iheXdsFileInfo.setPatientId(patientId);
		iheXdsInfoFileList.add(iheXdsFileInfo);
		xdsDto.setIheXdsInfoList(iheXdsInfoFileList);
		DataDao dataDao = new DataDao();
		return dataDao.findFileData(xdsDto);
	}
	
	public static EbXMLExtrinsicObject getFileMineTypeByDocumentID(String id,List<EbXMLExtrinsicObject> list) {
		EbXMLExtrinsicObject ebXMLExtrinsicObject=null;
		for(EbXMLExtrinsicObject o:list) {
			if(o.getId().equals(id)) {
				ebXMLExtrinsicObject=o;
				break;
			}
		}
		return ebXMLExtrinsicObject;
	
	}
	
	public static EbXMLRegistryPackage getEbXMLRegistryPackageByID(String id,List<EbXMLRegistryPackage> list) {
		EbXMLRegistryPackage ebXMLRegistryPackage=null;
		for(EbXMLRegistryPackage o:list) {
			if(o.getId().equals(id)) {
				ebXMLRegistryPackage=o;
				break;
			}
		}
		return ebXMLRegistryPackage;
	
	}
	
	public static EbXMLAssociation getEbXMLAssociationByID(String id,List<EbXMLAssociation> list) {
		EbXMLAssociation ebXMLAssociation=null;
		for(EbXMLAssociation o:list) {
			if(o.getId().equals(id)) {
				ebXMLAssociation=o;
				break;
			}
		}
		return ebXMLAssociation;
	
	}
}
